---
# Copyright 2017, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

- name: Pull systemd version
  command: "systemctl --version"
  changed_when: false
  register: systemd_version
  delegate_to: "{{ physical_host }}"
  tags:
    - skip_ansible_lint
    - always

- name: Set facts
  set_fact:
    nspawn_systemd_version: "{{ systemd_version.stdout_lines[0].split()[-1] }}"
  tags:
    - always

- name: Escape quote container name
  command: "systemd-escape {{ inventory_hostname }}"
  changed_when: false
  register: systemd_escape
  delegate_to: "{{ physical_host }}"
  tags:
    - skip_ansible_lint
    - always

- name: Ensure mount directories exists (container)
  file:
    path: "{{ item['mount_path'] }}"
    state: "directory"
  with_items:
    - "{{ list_of_bind_mounts | default([]) }}"
  delegate_to: "{{ physical_host }}"
  when:
    - not is_metal | bool
  tags:
    - common-nspawn

- name: Ensure mount directories exists (physical host)
  file:
    path: "{{ item['bind_dir_path'] }}"
    state: "directory"
  with_items:
    - "{{ list_of_bind_mounts | default([]) }}"
  when:
    - not is_metal | bool
  tags:
    - common-nspawn

- name: Create container bind mount config
  lineinfile:
    dest: "/etc/systemd/nspawn/{{ inventory_hostname }}.nspawn"
    line: "Bind={{ item['mount_path'] }}:{{ item['bind_dir_path'] }}"
    insertafter: "^Bind"
    backup: "true"
  with_items:
    - "{{ list_of_bind_mounts | default([]) }}"
  delegate_to: "{{ physical_host }}"
  register: _ec
  when:
    - not is_metal | bool
    - nspawn_systemd_version | int > 219
  tags:
    - common-nspawn

- name: Create container bind mount config (old)
  block:
    - name: Get ExecStart from config
      shell: >-
        grep -w '^ExecStart=/usr/bin/systemd-nspawn'
        /etc/systemd/system/systemd-nspawn@$(/usr/bin/systemd-escape {{ inventory_hostname }}).service
      delegate_to: "{{ physical_host }}"
      register: _ec_old_start
      changed_when: false

    - name: set flag fact
      set_fact:
        nspawn_flags: "{{ _ec_old_start.stdout.split('ExecStart=/usr/bin/systemd-nspawn')[-1] }}"
        nspawn_extra_flags: "{% for item in list_of_bind_mounts %} --bind={{ item['mount_path'] }}:{{ item['bind_dir_path'] }}{% endfor %}"

    - name: set flag list
      set_fact:
        nspawn_flag_list: "{{ nspawn_flags.split() | union(nspawn_extra_flags.split()) | unique }}"

    - name: Add line in container start config
      lineinfile:
        dest: "/etc/systemd/system/systemd-nspawn@{{ systemd_escape.stdout }}.service"
        line: "ExecStart=/usr/bin/systemd-nspawn {{ nspawn_flag_list | join(' ') }}"
        regexp: "^ExecStart"
        backup: "true"
      delegate_to: "{{ physical_host }}"
      register: _ec
  when:
    - not is_metal | bool
    - list_of_bind_mounts | default([])
    - nspawn_systemd_version | int < 220
  tags:
    - common-nspawn

- name: Restart container
  systemd:
    name: "systemd-nspawn@{{ systemd_escape.stdout }}"
    state: restarted
  register: _container_restart
  until: _container_restart | success
  retries: 3
  delay: 5
  delegate_to: "{{ physical_host }}"
  when:
    - _ec | changed
  tags:
    - common-nspawn

- name: Wait for container connectivity
  wait_for_connection:
    delay: 3
    timeout: 60
  when:
    - _container_restart | changed
  tags:
    - common-nspawn
